package com.guorui.servlet;

import com.guorui.bean.RealEstate;
import com.guorui.bean.Users;
import com.guorui.service.UsersServiece;
import com.guorui.service.impl.UsersServieceImpl;
import com.guorui.utils.PageUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet("/Servlet")
public class Servlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String method = request.getParameter("method");
        try {
            //得到当前类的反射对象
            Class cla = this.getClass();
            Method declaredMethod = cla.getDeclaredMethod(method, HttpServletRequest.class, HttpServletResponse.class);
            declaredMethod.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    private UsersServiece usersServiece = new UsersServieceImpl();

    //登录后验证用户信息
    public void yanzheng(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cardid = request.getParameter("cardid");
        String password = request.getParameter("password");
        Users yanzhen = usersServiece.yanzhen(cardid, password);
        PrintWriter out = response.getWriter();
        if (yanzhen == null) {
            out.print("<script>alert('登陆失败，请重试');location.href='login.jsp'</script>");

        } else {
            //     String name = yanzhen.getName();
            request.setAttribute("username", yanzhen.getName());
            request.getRequestDispatcher("login_success.jsp").forward(request, response);
        }
    }

    //注册
    public void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cardid = request.getParameter("cardid");
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        Users rigistfindid = usersServiece.rigistfindid(cardid);

        //验证用户是否存在
        if (rigistfindid == null) {
            Users users = new Users();
            users.setCardId(cardid);
            users.setName(name);
            users.setSex("男");
            users.setLastModifyTime("2019-11-1");
            users.setStatus(1);
            users.setPassword(password);
            int addusers = usersServiece.addusers(users);
            request.setAttribute("username", name);
            request.getRequestDispatcher("login_success.jsp").forward(request, response);

        } else {
            PrintWriter out = response.getWriter();
            out.print("<script>alert('用户已存在，请直接登录');location.href='login.jsp'</script>");
        }

    }


    //查询所有的用户信息
    public void findall(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PageUtils<RealEstate> pageUtils = new PageUtils<>();
        String index1 = request.getParameter("index");
        int index = 1;
        if(index1 != null){
            index = Integer.parseInt(index1);
        }
        pageUtils.setPageIndex(index);



        String note = request.getParameter("note");
        request.setAttribute("note",note);
        //用户名和身份证
        String dt = request.getParameter("Dt");
        int i = 0;
        if (dt != null) {
            i = Integer.parseInt(dt);
        }
        request.setAttribute("dtid",i);

        //获取总条数
        long total1 = usersServiece.total(note, i);
        int total = (int)total1;
        request.setAttribute("totalCount",total);
        pageUtils.setTotalCount(total);
        //获取起始条数
        int first = (pageUtils.getPageIndex() - 1)*pageUtils.getPageSize();

        List<RealEstate> findall = usersServiece.findall(note, i,first,pageUtils.getPageSize());
//        for (RealEstate realEstate : findall) {
//            System.out.println(realEstate.getBuildTime() + "  " + realEstate.getProjectName() + "  " + realEstate.getCardId() + "   " + realEstate.getHouseType());
//        }

        pageUtils.setDataList(findall);
        request.setAttribute("pageUtils", pageUtils);
        request.getRequestDispatcher("show.jsp").forward(request, response);

    }

    //增加
    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cardid = request.getParameter("cardid");
        String project = request.getParameter("project");
        String address = request.getParameter("address");
        String type = request.getParameter("type");
        String area = request.getParameter("area");
        String time = request.getParameter("time");
        RealEstate realEstate = new RealEstate();
        realEstate.setCardId(cardid);
        realEstate.setProjectName(project);
        realEstate.setAddress(address);
        realEstate.setHouseType(type);
        int i = Integer.parseInt(area);
        realEstate.setArea(i);
        realEstate.setBuildTime(time);

        int add = usersServiece.add(realEstate);
        request.getRequestDispatcher("Servlet?method=findall").forward(request,response);

    }

    //删除
    public void delectServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        int delete = usersServiece.delete(Integer.parseInt(id));
        //往页面写，为响应，所以用out对象得用response来得到
        PrintWriter out = response.getWriter();
        if(delete == 1){
            out.print("<script>alert('删除成功');location.href='Servlet?method=findall'</script>");
        }else {
            out.print("<script>alert('删除失败');location.href='Servlet?method=findall'</script>");
        }

    }

    //根据id回显
    public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        RealEstate byId = usersServiece.findById(Integer.parseInt(id));
        request.setAttribute("byId",byId);
        request.getRequestDispatcher("update.jsp").forward(request,response);

    }


    //修改
    public void updateServlet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String cardid = request.getParameter("cardid");
        String type = request.getParameter("type");
        String area = request.getParameter("area");
        String time = request.getParameter("time");
        RealEstate realEstate = new RealEstate();
        realEstate.setId(Integer.parseInt(id));
        realEstate.setCardId(cardid);
        realEstate.setProjectName(name);
        realEstate.setHouseType(type);
        realEstate.setArea(Integer.parseInt(area));
        realEstate.setBuildTime(time);
        realEstate.setAddress("北京");

        int update = usersServiece.update(realEstate);
        request.getRequestDispatcher("Servlet?method=findall").forward(request,response);
    }
}
